<?php

/**
 * @author Samrat Khan & Sadik Sarfaraz - Apr 16, 2013
 */
require('top.inc.php');
    $formLoad = new Form();
?>
<?php
if(isset($_SESSION['user']) && $_SESSION['user']['utype'] == 1 || $_SESSION['user']['utype'] == 2 || $_SESSION['user']['utype'] == 3 || $_SESSION['user']['utype'] == 4) {
    $sess_cusID = $_SESSION['user']['cus_id'];
    if(isset($_POST['addReturn'])) {
        $query = "INSERT INTO returns ( serialno, project_id, return_rma, pickuprequestdate, physicallyreceivedate, productid, description, revisionstate, boxno,  
                    uom,  return_quantity, manufacturingdate, returningstatus, receivedfrom, siteid, warehouse_id, employee_id, docketdate, return_docketnumber, 
                    remarks, userid, cus_id, inserteddate )
             
            VALUES (:serialno, :project_id, :return_rma, :pickuprequestdate, :physicallyreceivedate, :productid, :description, :revisionstate, :boxno, 
                :uom,  :return_quantity, :manufacturingdate, :returningstatus, :receivedfrom, :siteid, :warehouse_id, :employee_id, 
                :docketdate, :return_docketnumber, :remarks, :userid, :cus_id, :inserteddate)";

        $query_params = array(
            ':serialno'                 => $_POST['serialno'],
            ':project_id'               => $_POST['project_id'],
            ':return_rma'               => $_POST['return_rma'],
            ':pickuprequestdate'        => $_POST['pickuprequestdate'],
            ':physicallyreceivedate'    => $_POST['physicallyreceivedate'],
            ':productid'                => $_POST['productid'],
            ':description'              => $_POST['description'],
            ':revisionstate'            => $_POST['revisionstate'],
            ':boxno'                    => $_POST['boxno'],
            ':uom'                      => $_POST['uom'],
            ':return_quantity'          => $_POST['return_quantity'],
            ':manufacturingdate'        => $_POST['manufacturingdate'],
            ':returningstatus'          => $_POST['returningstatus'],
            ':receivedfrom'             => $_POST['receivedfrom'],
            ':siteid'                   => $_POST['siteid'],
            ':warehouse_id'             => $_POST['warehouse_id'],
            ':employee_id'                => $_POST['employee_id'],
            ':docketdate'               => $_POST['docketdate'],
            ':return_docketnumber'      => $_POST['return_docketnumber'],
            ':remarks'                  => $_POST['remarks'],
            ':userid'                   => $_POST['userid'],
            ':cus_id'                   => $_POST['cus_id'],
            ':inserteddate'             => date('Y-m-d H:i:s')
        );
        
        try {
            $stmt = $db->prepare($query);
            $result = $stmt->execute($query_params);
        } catch (PDOException $ex) {
            die("Failed to run query: " . $ex->getMessage());
        }
        if($result) {
            echo $msg['4'];
        }
    } else {
?>
<form class="form-horizontal" action="" method="post" id="addReturn">
    <div class="control-group">
        <label class="control-label" for="serialno"><?php echo $stock_in['serialno']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("serialno", "span2", "faulty_in_sn", $stock_in['serialno']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="project_id"><?php echo $projects['project_id']; ?></label>
        <div class="controls">
            <select name="project_id">
                <?php echo getProjects(cus_id, $sess_cusID, $db) ?>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="return_rma"><?php echo $return['return_rma']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("return_rma", "span2", "", $return['return_rma']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="pickuprequestdate"><?php echo $return['pickuprequestdate']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("pickuprequestdate", "span2", "pickuprequestdate", $return['pickuprequestdate']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="physicallyreceivedate"><?php echo $return['physicallyreceivedate']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("physicallyreceivedate", "span2", "physicallyreceivedate", $return['physicallyreceivedate']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="productid"><?php echo $return['productid']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("productid", "span2", "", $return['productid']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="description"><?php echo $return['description']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textArea("description", "span5", ""); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="revisionstate"><?php echo $return['revisionstate']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("revisionstate", "span2", "", $return['revisionstate']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="boxno"><?php echo $return['boxno']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("boxno", "span2", "", $return['boxno']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="uom"><?php echo $uom['uom_id']; ?></label>
        <div class="controls">
            <select name="uom">
                <?php echo getUOM($db); ?>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="return_quantity"><?php echo $return['return_quantity']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("return_quantity", "span2", "", $return['return_quantity']); ?>
        </div>
    </div>
    
    <div class="control-group">
        <label class="control-label" for="manufacturingdate"><?php echo $return['manufacturingdate']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("manufacturingdate", "span2", "manufacturingdate", $return['manufacturingdate']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="returningstatus"><?php echo $return_status['status_id']; ?></label>
        <div class="controls">
            <select name="returningstatus">
                <?php echo getReturningStatus($db); ?>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="receivedfrom"><?php echo $return['receivedfrom']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("receivedfrom", "span2", "", $return['receivedfrom']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="siteid"><?php echo $return['siteid']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("siteid", "span2", "", $return['siteid']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="warehouse_id"><?php echo $warehouses['warehouse_id']; ?></label>
        <div class="controls">
            <select name="warehouse_id">
                <?php echo getWarehousesCode($db); ?>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="employeename"><?php echo $employee['employeename']; ?></label>
        <div class="controls">
            <select name="employee_id">
                <?php echo getEmployees($db); ?>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="docketdate"><?php echo $return['docketdate']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("docketdate", "span2", "docketdate", $return['docketdate']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="return_docketnumber"><?php echo $return['return_docketnumber']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textField("return_docketnumber", "span2", "", $return['return_docketnumber']); ?>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="remarks"><?php echo $return['remarks']; ?></label>
        <div class="controls">
            <?php echo $formLoad->textArea("remarks", "span5", ""); ?>
        </div>
    </div>
       
    <input type="hidden" name="userid" value="<?php echo $_SESSION['user']['id']; ?>" />
    <input type="hidden" name="cus_id" value="<?php echo $_SESSION['user']['cus_id']; ?>" />
    
    <div class="control-group">
        <label class="control-label" for="inputEmail">&nbsp;</label>
        <div class="controls"><input class="btn btn-primary" type="submit" value="Add Item" name="addReturn" /></div>
    </div>
    
    
    
</form>
<?php
    }
} else {
    header("Location: login.php");
}
?>
<?php
    include ('bottom.inc.php');
?>